package com.student.xujiang.myallstudent.eventbus;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.student.xujiang.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 关于Eventbus 有时间可以看看
 * http://www.cnblogs.com/ouyanliu/p/5744862.html
 *
 * */

public class EventBusMainActivity extends AppCompatActivity {

    @BindView(R.id.bt_eventbus_send)
    Button btEventbusSend;
    @BindView(R.id.bt_eventbus_sticky)
    Button btEventbusSticky;
    @BindView(R.id.tv_eventbus_result)
    TextView tvEventbusResult;
    @BindView(R.id.activity_event_bus_main)
    RelativeLayout activityEventBusMain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_bus_main);
        ButterKnife.bind(this);

        initData();

    }

    private void initData() {
        //1、注册广播
        EventBus.getDefault().register(EventBusMainActivity.this);
    }

    @OnClick({R.id.bt_eventbus_send, R.id.bt_eventbus_sticky})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_eventbus_send:
                //跳转到发送页面
                Intent intent =  new Intent(EventBusMainActivity.this,EventBusSendActivity.class);
                startActivity(intent);

                break;
            case R.id.bt_eventbus_sticky:
                //发送粘性事件到跳转到发送页面
                //12、发送粘性事件

                EventBus.getDefault().postSticky(new StickyEvent("我是粘性事件"));

                Intent i = new Intent(EventBusMainActivity.this,EventBusSendActivity.class);
                startActivity(i);

                break;
        }
    }

    /**
     * http://blog.csdn.net/angcyo/article/details/48166849
     *
     * @Subscribe(threadMode = ThreadMode.MainThread) //在ui线程执行
     * public void onUserEvent(UserEvent event) {
     * }
     *
     * @Subscribe(threadMode = ThreadMode.BackgroundThread) //在后台线程执行
     * public void onUserEvent(UserEvent event) {
     * }
     *
     * @Subscribe(threadMode = ThreadMode.Async) //强制在后台执行
     * public void onUserEvent(UserEvent event) {
     * }
     *
     * @Subscribe(threadMode = ThreadMode.PostThread) //默认方式, 在发送线程执行
     * public void onUserEvent(UserEvent event) {
     * }
     *
     * */

    // 5 接收消息 1 参数是消息类 2加注解 表示主线程执行
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void MessageEventBus(MessageEvent event){
        //显示接收的消息
        tvEventbusResult.setText(event.name);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //2、解注册
        EventBus.getDefault().unregister(EventBusMainActivity.this);
    }
}
